package dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

public class BaseDao {
	private String driver = "com.mysql.jdbc.Driver";
	private String url = "jdbc:mysql://localhost:3306/db_init?useUnicode=true&characterEncoding=utf8";
	private String user = "root";
	private String pwd = "123456";
 
	public Connection getConnection() {
		Connection conn = null;
		try {
			Class.forName(driver);
			conn = DriverManager.getConnection(url, user, pwd);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
	}
 
	public void closeAll(ResultSet rs, Statement stmt, Connection conn) {
		try {
			if(rs!=null){
				rs.close();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
           if(stmt!=null){
        	   stmt.close();
           }
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
		 if(conn!=null){
			 conn.close();
		 }
		} catch (Exception e) {
			e.printStackTrace();
		}

	} 
	public int execeteUpdata(String sql,Object[]objects){
		Connection conn=null;
		PreparedStatement pstm=null;
		int count=-1;
		try {
			conn=getConnection();
			pstm=conn.prepareStatement(sql);
			for (int i = 0; i < objects.length; i++) {
				pstm.setObject(i+1, objects[i]);
			}
			count=pstm.executeUpdate();
		} catch (Exception e) {
		e.printStackTrace();
		}finally{
			
			
		}
		return count;
	}
}
